package com.qf.controller;

import com.netflix.discovery.EurekaClient;
import com.qf.feign.SearchFeign;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author fy
 * @version V1.0
 * @Project workspace
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/6 19:06
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private EurekaClient eurekaClient;

    /**
     * resttemplate是spring封装的java服务端发送http协议的远程调用工具
     */
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private SearchFeign searchFeign;

    /**
     * 测试远程调用底层原理
     * @return
     */
    @RequestMapping("/consumer")
    public String consumer(){
        //1.通过EurekaClient对象获取search搜索微服务的对象信息
//        InstanceInfo info = eurekaClient.getNextServerFromEureka("QF-SEARCH", false);
        //2.从搜索微服务的对象信息中获取搜索微服务的ip地址和端口号
//        String url = info.getHomePageUrl();
//        System.out.println("=====url======"+url);
        //3.通过restTemplate对象发送http协议远程调用
//        String str = restTemplate.getForObject(url + "/search/test", String.class);
        String str = restTemplate.getForObject( "http://QF-SEARCH/search/test", String.class);

        //4.返回结果
        System.out.println("==============="+str);
        return str;


    }

    /**
     * 测试feign远程调用接口
     * @return
     */
    @RequestMapping("/testFeign")
    public String testFeign(){
        String str = searchFeign.test1();
        System.out.println("========="+str);
        return str;
    }

    @RequestMapping("/testUser")
    public User testFindUser(){
        User user = searchFeign.findUserById(1);
        return user;
    }

}
